<?php

namespace App\Http\Controllers\Estate;

use App\Http\Controllers\Controller;
use App\Models\ElectricPaymentOrder;
use App\Models\ElectricPlatformOwner;
use Illuminate\Http\Request;

class FinanceIndexController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {

        $where[] = ['status' , 2];
        $data=$request->query();

        if (isset($data['status']))
        {
            $where['feeType'] = $data['status'];
        }
        $query = ElectricPlatformOwner::query()
            ->with(["payment_order",'platform_agent','fee_log','community'])
            ->paginate($request->per_page ?? 30);

        if (isset($data['change']))
        {
            $totalQuery=ElectricPaymentOrder::query()
                ->where($where)
                ->whereDate('create_time', '>=',
                    date("Y-m-d H:i:s",strtotime($data['change'][0])))
                ->whereDate('create_time', '<=',
                    date("Y-m-d H:i:s",strtotime($data['change'][1])))
                ->get();
        }else{
            $totalQuery=ElectricPaymentOrder::query()
                ->where($where)
                ->get();
        }
        $totals = sprintf("%.2f", $totalQuery->sum('paymentMoney'));
        foreach ($query as $k=>$v){
            $payment_order=$v['payment_order'];
            $fee_log=$v['fee_log'];
            unset($v['payment_order'],$v['fee_log']);
            $query[$k]=$v;
            foreach ($payment_order as $k1 => $v1)
            {
                    if (isset($data['status']))
                    {
                        if ($data['status']==$v1['feeType'])
                        {
                            switch ($v1['feeType'])
                            {
                                case 1:
                                    $query[$k]['ele_fee'] = $v1['paymentMoney'];
                                    break;
                                case 2:
                                    $query[$k]['water_fee'] = $v1['paymentMoney'];
                                    break;
                                case 3:
                                    $query[$k]['pro_fee'] = $v1['paymentMoney'];
                                    break;
                            }

                            foreach ($fee_log as $k2=>$v2){

                                if ($data['status']==$v2['type'])
                                {
                                    switch ($v2['type']) {
                                        case 1:
                                            $query[$k]['ele_deduction'] = $v2['bill_balance'];
                                            break;
                                        case 2:
                                            $query[$k]['water_deduction'] = $v2['bill_balance'];
                                            break;
                                        case 3:
                                            $query[$k]['pro_deduction'] = $v2['bill_balance'];
                                            break;
                                    }
                                }
                            }
                        }else{
                            unset($v);
                        }

                    }else{
                        switch ($v1['feeType'])
                        {
                            case 1:
                                $query[$k]['ele_fee'] = $v1['paymentMoney'];
                                break;
                            case 2:
                                $query[$k]['water_fee'] = $v1['paymentMoney'];
                                break;
                            case 3:
                                $query[$k]['pro_fee'] = $v1['paymentMoney'];
                                break;
                        }
                        foreach ($fee_log as $k2=>$v2){
                            switch ($v2['type']) {
                                case 1:
                                    $query[$k]['ele_deduction'] = $v2['bill_balance'];
                                    break;
                                case 2:
                                    $query[$k]['water_deduction'] = $v2['bill_balance'];
                                    break;
                                case 3:
                                    $query[$k]['pro_deduction'] = $v2['bill_balance'];
                                    break;
                            }
                        }
                    }
            }


        }

        return $this->success([$query,$totals]);
    }

}
